package com.clas.liu.myapplication.model.impl;

import android.os.Handler;

import com.clas.liu.myapplication.model.entity.hotKeys.HotKeysRoot;
import com.clas.liu.myapplication.model.entity.info.InfoRoot;
import com.clas.liu.myapplication.model.entity.searchUser.SearchUserRoot;
import com.clas.liu.myapplication.model.event.SearchEvent;
import com.clas.liu.myapplication.model.i.ISearchModel;
import com.clas.liu.myapplication.network.NetworkInterfaces;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by liu on 2019/5/19.
 */

public class SearchModel extends BaseModel implements ISearchModel {

    public SearchModel(Handler handler) {
        super(handler);
    }

    @Override
    public void searchInfo(String s, int page_no, int page_size) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                SearchEvent event = new SearchEvent();
                event.setWhat(SearchEvent.SEARCH_INFO_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    InfoRoot root = gson.fromJson(response.body().string(), InfoRoot.class);  //json解析
                    SearchEvent event = new SearchEvent();                        //事件
                    event.setWhat(SearchEvent.SEARCH_INFO_OK);                         //事件的设置
                    event.setInfoRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.searchInfo(s,page_no,page_size,callback);
    }

    @Override
    public void searchUser(String s, int page_no, int page_size) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                SearchEvent event = new SearchEvent();
                event.setWhat(SearchEvent.SEARCH_USER_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    SearchUserRoot root = gson.fromJson(response.body().string(), SearchUserRoot.class);  //json解析
                    SearchEvent event = new SearchEvent();                        //事件
                    event.setWhat(SearchEvent.SEARCH_USER_OK);                         //事件的设置
                    event.setSearchUserRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.searchUser(s,page_no,page_size,callback);
    }

    @Override
    public void getHotKeys() {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                SearchEvent event = new SearchEvent();
                event.setWhat(SearchEvent.GET_HOT_KEYS_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    HotKeysRoot root = gson.fromJson(response.body().string(), HotKeysRoot.class);  //json解析
                    SearchEvent event = new SearchEvent();                        //事件
                    event.setWhat(SearchEvent.GET_HOT_KEYS_OK);                         //事件的设置
                    event.setHotKeysRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getHotKeys(callback);

    }

    @Override
    protected NetworkInterfaces getNetworkInterface() {
        return new NetworkInterfaces();
    }
}
